package com.ziq.zcamp.dialog.share

import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.ziq.base.extension.BindingViewHolder
import com.ziq.base.extension.*
import com.ziq.zcamp.databinding.ItemDialogShareActionBinding
import com.ziq.zcamp.dialog.share.ActionItem

class ActionAdapter() : RecyclerView.Adapter<BindingViewHolder<ItemDialogShareActionBinding>>() {

        private val dataList: MutableList<ActionItem> = mutableListOf()
        fun setData(list: List<ActionItem>) {
            dataList.clear()
            dataList.addAll(list)
            notifyDataSetChanged()
        }

        override fun getItemCount(): Int {
            return dataList.size
        }

        var actionCallBack : ((action:String)->Unit)? = null

        fun setActionCallback(callback : (action:String)->Unit) {
            actionCallBack = callback
        }

        override fun onCreateViewHolder(
            parent: ViewGroup,
            viewType: Int
        ): BindingViewHolder<ItemDialogShareActionBinding> {
            return createBindingViewHolder(parent)
        }

        override fun onBindViewHolder(
            holder: BindingViewHolder<ItemDialogShareActionBinding>,
            position: Int
        ) {
            val item = dataList[position]
            holder.binding.icon.setImageResource(item.iconRes)
            holder.binding.text.text = item.text
            holder.itemView.click {
                actionCallBack?.invoke(item.text)
            }
        }
    }